package org.myspringframework.mvc.processor.impl;

import lombok.extern.slf4j.Slf4j;
import org.myspringframework.mvc.RequestProcessorChain;
import org.myspringframework.mvc.processor.RequestProcessor;

/**
 * org.myspringframework.mvc.processor.impl <br>
 * 请求预处理，包括编码以及路径处理
 *
 * @author jaymin
 * @since 2021/5/22
 */
@Slf4j
public class PreRequestProcessor implements RequestProcessor {
    @Override
    public boolean proceed(RequestProcessorChain requestProcessorChain) throws Exception {
        requestProcessorChain.getHttpServletRequest().setCharacterEncoding("UTF-8");
        // replace "/" -> ""
        String requestPath = requestProcessorChain.getRequestPath();
        if (requestPath.length() > 1 && requestPath.endsWith("/")) {
            requestProcessorChain.setRequestPath(requestPath.substring(0, requestPath.length() - 1));
        }
        log.info("preprocess request-method:{},path:{}", requestProcessorChain.getRequestMethod(), requestProcessorChain.getRequestPath());
        return true;
    }
}
